package jp.sourceforge.akjrcp.videotools.subtitlemaker;

import java.io.File;

import jp.sourceforge.akjrcp.commons.util.FileNameUtils;

import jp.sourceforge.akjrcp.util.TimeUtils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.draw2d.Figure;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public  class SimpleImageChecker extends Thread implements IImageChecker{
	 private boolean done;
	 private boolean cancelled;
	 public static Log log=LogFactory.getLog(SimpleImageChecker.class);
	
	 private int index=1;
	 private long timeout=1000*60*10;//10minute
	 private Shell shell;
     protected ThumbnailPreference preference;
	 protected SelectableFigureList parentFigure;
	 public SimpleImageChecker(ThumbnailPreference preference,Shell shell,SelectableFigureList parentFigure){
		 this.preference=preference;
		 this.shell=shell;
		 this.parentFigure=parentFigure;

	 }
	 public void run(){
		 Display display=shell.getDisplay();
		 LOOP:while(!cancelled){
			 String numStr=FileNameUtils.digited(index,5);
			 String fileName=preference.getBaseName()+numStr+"."+preference.getExtension();
			 File file=new File(preference.getBaseDir(),fileName);
			 long lasttime=System.currentTimeMillis();
			 if(file.exists() && file.lastModified()+250<lasttime){//dont read too early
				 try{
			
				 //TODO make thumbnail
				 long time=(long) ((1/preference.getFps())*(index-1));
				 //log.trace(time+":"+index);
				// String label=TimeUtils.toTimeText(time*1000,true,true,true,false);
				 
				 
				// ImageAndLabelFigure figure=new ImageAndLabelFigure(label,ImageUtils.createThumbnail(img, 160, 120));
				 //figure.setFileName(fileName);
				/*
				 LabelAndName label=new LabelAndName(ImageUtils.createThumbnail(img, 160, 120));
				 label.setName(fileName);//for get
				 img.dispose();
				 */
				 if(!cancelled){
				 int size=parentFigure.getChildren().size();
				if(size>index-1){
					 FigureUtils.syncRepaintFigure(display, (Figure)parentFigure.getChildren().get(index-1));
					
				}
				 }
				// PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell().getDisplay().syncExec(new AddFigure(explorerFigure,label));
				 index++;
				 lasttime=System.currentTimeMillis();
				 }catch(Exception e){
					 log.warn("readfaild:"+file.getAbsolutePath());
					 //final break
					 if(lasttime+timeout<System.currentTimeMillis()){
						 break LOOP;//timeout break;
					 }
					 try {
							Thread.sleep(250);
						} catch (InterruptedException e2) {
							// TODO �����������ꂽ catch �u���b�N
							e.printStackTrace();
						}
				 }
			 }else{
				 if(lasttime+timeout<System.currentTimeMillis()){
					 break LOOP;//timeout break;
				 }
				 
				 if(done){
					 log.trace("done");
					 break;
				 	}
				 try {
					Thread.sleep(250);
				} catch (InterruptedException e) {
					// TODO �����������ꂽ catch �u���b�N
					e.printStackTrace();
				}
			 }
		 }
		 //check dir
		
		
		 //progress bar ..sync
		 //add figure
	 }

	public boolean isDone() {
		return done;
	}
	public void setDone(boolean done) {
		this.done = done;
	}
	public boolean isCancelled() {
		return cancelled;
	}
	public void setCancelled(boolean cancelled) {
		this.cancelled = cancelled;
	}



}
